package StudyCode.summary.myfile;

import java.io.IOException;
import java.nio.file.*;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class UsingWatchEventsFile {
	public static void main(String[] args) {
		try {
			FileSystem fileSystem = FileSystems.getDefault();
			WatchService watchService = fileSystem.newWatchService();
			Path directory = Paths.get("E:/Copy");
			WatchEvent.Kind<?>[] events = {
					StandardWatchEventKinds.ENTRY_CREATE,
					StandardWatchEventKinds.ENTRY_DELETE,
					StandardWatchEventKinds.ENTRY_MODIFY};
			directory.register(watchService, events);
			while (true) {
				System.out.println("等待 watch event");
				WatchKey watchKey = watchService.take();

				System.out.println("路径开始被监听: " + watchKey.watchable());
				System.out.println();

				if (watchKey.isValid()) {
					for (WatchEvent<?> event : watchKey.pollEvents()) {
						System.out.println("类型: " + event.kind());
						System.out.println("内容: " + event.context());
						System.out.println("数量: " + event.count());
						System.out.println();
					}
				}
			}

		} catch (IOException | InterruptedException ex) {
			ex.printStackTrace();
		}

	}
}
